﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.BL;

namespace ADC.Web.Admin
{
    public partial class ListUser : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ListUser");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitData();
            }
        }

        private void InitData()
        {
            try
            {
                var users = UserFactory.GetAll();
                GridViewUser.DataSource = users;
                GridViewUser.DataBind();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                var lblUserID = (Label)GridViewUser.Rows[e.RowIndex].FindControl("lblUserID");
                if (lblUserID == null || string.IsNullOrEmpty(lblUserID.Text)) return;
                // Delete user
                UserFactory.Delete(Convert.ToInt32(lblUserID.Text));
                InitData();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewUser_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                var lblUserID = (Label)GridViewUser.Rows[e.NewEditIndex].FindControl("lblUserID");
                if (lblUserID != null && !string.IsNullOrEmpty(lblUserID.Text))
                {
                    Response.Redirect("ManageUser.aspx?Mode=1&ID=" + lblUserID.Text, false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }
    }
}